#include <iostream>

using namespace std;

const int N = 1e6 + 10;
int l[N], r[N];
int n;

int dfs(int u)
{
	if(u == 0)
	    return 0;
	int a = dfs(l[u]);
	int b = dfs(r[u]);
	return max(a, b) + 1;
}
int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		cin >> l[i] >> r[i];
	}
	int cnt = dfs(1);
	cout << cnt << endl;
	return 0;
}
